<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #title span{
            margin-right: 33px;
            display: inline-block;
        }
        #content {
            width: 100px;
            height: 100px;
            border: 1px solid orchid;
            position: relative;
        }
        #content div {
            width: 100%;
            height: 100%;
            position: absolute;
        }
        #content div:first-child{
            background-color: palegreen;
           
        }
        #content div:last-child{
            background-color: orangered;
            display: none;
        }
        .active{
            color: orangered;
        }
    </style>
</head>
<body>
    <div id="title">
        <span class="active">登录</span> 
        <span>注册</span>
    </div>
    <div id="content">
        <div>登录内容</div>
        <div>注册内容</div>
    </div>
    <script>
        var title = document.querySelectorAll("#title span");
        var content = document.querySelectorAll("#content>div");
        console.log(content)
        for(var i = 0;i<title.length;i++){
            title[i].index = i;
            console.log(title[i].index)
            title[i].onclick = function(){
                for(var i = 0;i<title.length;i++){
                    title[i].classList.remove("active")
                    content[i].style.display = "none"
                }
                this.classList.add('active');
                content[this.index].style.display = "block"
                console.log(this.index)
            }
        }
        // var lis = document.querySelectorAll('ul li');
        // var content = document.querySelectorAll('#content>div')
        // for(var i = 0;i<lis.length;i++){
        //     lis[i].index = i;//lis[i].setAttribute('index',i)
        //     lis[i].onclick = function(){
        //         for(var i=0;i<lis.length;i++){
        //             lis[i].classList.remove('active');
        //             content[i].style.display = "none";
        //         }
        //         this.classList.add('active');
        //         content[this.index].style.display = "block";  
        //       //var index =this.getAttribute('index');
        //       // content[index].style.display = "block";  
        //     }

        //     }
    </script>
</body>
</html>